package org.NooLab.itexx.browser;

import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.bidimap.DualHashBidiMap;

public class KeyCodes {

	
	BidiMap keyboardLabels = new DualHashBidiMap();
	private int keyCode;
	private String[] functionKeys;
	private int lastModKeyCode;
	
	
	// ========================================================================
	public KeyCodes(int keycode, int lastkeycode){
		keyCode = keycode;
		lastModKeyCode = lastkeycode;
		
		initialize();
	}
	// this should be more global, perhaps to be imported from a routine more up in the hierarchy
	private void initialize() {
		// TODO Auto-generated method stub
		keyboardLabels.put(65, "a") ;
		keyboardLabels.put(66, "b") ;
		keyboardLabels.put(67, "c") ;
		keyboardLabels.put(68, "d") ;
		keyboardLabels.put(69, "e") ;
		keyboardLabels.put(70, "f") ;
		keyboardLabels.put(71, "g") ;
		keyboardLabels.put(72, "h") ;
		keyboardLabels.put(73, "i") ;
		keyboardLabels.put(74, "j") ;
		keyboardLabels.put(75, "k") ;
		keyboardLabels.put(76, "l") ;
		keyboardLabels.put(77, "m") ;
		keyboardLabels.put(78, "n") ;
		keyboardLabels.put(79, "o") ;
		keyboardLabels.put(80, "p") ;
		keyboardLabels.put(81, "q") ;
		keyboardLabels.put(82, "r") ;
		keyboardLabels.put(83, "s") ;
		keyboardLabels.put(84, "t") ;
		keyboardLabels.put(85, "u") ;
		keyboardLabels.put(86, "v") ;
		keyboardLabels.put(87, "w") ;
		keyboardLabels.put(88, "x") ;
		keyboardLabels.put(89, "y") ;
		keyboardLabels.put(90, "z") ;
		
		keyboardLabels.put(48, "0") ;
		keyboardLabels.put(49, "1") ;
		keyboardLabels.put(50, "2") ;
		keyboardLabels.put(50, "3") ;
		keyboardLabels.put(50, "4") ;
		keyboardLabels.put(50, "5") ;
		keyboardLabels.put(50, "6") ;
		keyboardLabels.put(50, "7") ;
		keyboardLabels.put(50, "8") ;
		keyboardLabels.put(50, "9") ;
		
		keyboardLabels.put(16, "shift") ;
		keyboardLabels.put(17, "ctrl") ;
		keyboardLabels.put(18, "alt") ;
		keyboardLabels.put(8,  "del") ;
		keyboardLabels.put(10, "cr") ;
		keyboardLabels.put(27, "esc") ;
		keyboardLabels.put(20, "caps") ;
		
		
		keyboardLabels.put(524, "option") ;
		
		keyboardLabels.put(37, "left") ;
		keyboardLabels.put(38, "up") ;
		keyboardLabels.put(39, "right") ;
		keyboardLabels.put(40, "down") ;
		
		keyboardLabels.put(44, ",") ;
		keyboardLabels.put(46, ".") ;
		keyboardLabels.put(45, "-") ;
		
		keyboardLabels.put(153, "<") ;
		
		keyboardLabels.put(112, "f1") ;
		keyboardLabels.put(113, "f2") ;
		keyboardLabels.put(114, "f3") ;
		keyboardLabels.put(115, "f4") ;
		keyboardLabels.put(116, "f5") ;
		keyboardLabels.put(117, "f6") ;
		keyboardLabels.put(118, "f7") ;
		keyboardLabels.put(119, "f8") ;
		keyboardLabels.put(120, "f9") ;
		keyboardLabels.put(121, "f10") ;
		keyboardLabels.put(122, "f11") ;
		keyboardLabels.put(123, "f12") ;
		
		
	}
	// ========================================================================

	public KeyCodes actualCodes(int keycode, int lastkeycode) {
		keyCode = keycode;		
		lastModKeyCode = lastkeycode;
		return this;
	}

	public boolean is(String keyboardLabel, String... functionkeys) {
		// 
		boolean rB=false;
		
		if (functionkeys==null){
			functionKeys = new String[0];
		}else{
			functionKeys = new String[functionkeys.length] ;
			System.arraycopy(functionkeys, 0, functionKeys, 0, functionKeys.length);
		}
		
		rB = checkForLabel( keyboardLabel);
		
		if ((rB) && (functionKeys.length>0) && (functionKeys[0].length()>0)){
			rB = checkForFunction( functionKeys[0] );
		}
		
		return rB;
	}
	
	
	private boolean checkForFunction(String functionKeyLabel) {

		boolean rB=false;
		int mappedValue = -1;
		String mappedLabel = "";

		functionKeyLabel = functionKeyLabel.toLowerCase().trim() ;
		
		if (keyboardLabels.containsValue(functionKeyLabel.toLowerCase())){
			if (keyboardLabels.containsKey(this.lastModKeyCode)){
				mappedValue = (Integer) keyboardLabels.getKey( functionKeyLabel.toLowerCase());
				mappedLabel = ((String) keyboardLabels.get(lastModKeyCode)).toLowerCase();
				
				//rB = ((mappedValue==keyCode) && (mappedLabel.toLowerCase().contentEquals(functionKeyLabel.toLowerCase())));
				rB = ((mappedValue==lastModKeyCode) && (mappedLabel.toLowerCase().contentEquals(functionKeyLabel.toLowerCase())));
			}
		}

		
		return rB;
	}

	
	private boolean checkForLabel(String keyboardLabel) {

		boolean rB=false;
		int mappedValue = -1;
		String mappedLabel = "";

		keyboardLabel = keyboardLabel.toLowerCase().trim() ;
		
		if (keyboardLabels.containsValue(keyboardLabel)){
			if (keyboardLabels.containsKey(this.keyCode)){
				mappedValue = (Integer) keyboardLabels.getKey( keyboardLabel);
				mappedLabel = (String) keyboardLabels.get(keyCode);
				
				rB = ((mappedValue==keyCode) && (mappedLabel.contentEquals(keyboardLabel)));
			}
		}

		
		return rB;
	}
	
	
	
}
